
<%@ page import="ch.vd.dec.selt.petale.TypologiePrestation" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta name="layout" content="main" />
        <title><g:message code="typologiePrestation.edit" default="Edit TypologiePrestation" /></title>
		<g:javascript library="prototype" />
		
		<g:javascript>

			function updateSelect() {
				
				var zselectBase = document.getElementById('base.id');
				var zoptBase = zselectBase.options[zselectBase.selectedIndex];
				
				var zselectAut = document.getElementById('aut.id');
				var zoptAut = zselectAut.options[zselectAut.selectedIndex];
				
				var zselectCat = document.getElementById('cat.id');
				var zoptCat = zselectCat.options[zselectCat.selectedIndex];
				
				var zselectGrp = document.getElementById('grp.id');
				var zoptGrp = zselectGrp.options[zselectGrp.selectedIndex];
				
				document.getElementById('uniqueKey').value = ((zoptBase.value != 'null' && zoptBase.value != '') ? zoptBase.value :0) + '#' + ((zoptAut.value != 'null' && zoptAut.value != '') ? zoptAut.value :0) + '#' + ((zoptCat.value != 'null' && zoptBase.value != '') ? zoptCat.value :0);

				${remoteFunction(controller:"typologieAutorisation", action:"ajaxGetBaseLegale", params:"'aut.id=' + zoptAut.value + '&cat.id=' + zoptCat.value", onComplete:"updateTypologie(e, 'base.id', zoptBase.value)")};
				${remoteFunction(controller:"typologieAutorisation", action:"ajaxGetTypeAutorisation", params:"'base.id=' + zoptBase.value + '&cat.id=' + zoptCat.value", onComplete:"updateTypologie(e, 'aut.id', zoptAut.value)")};
				${remoteFunction(controller:"typologieAutorisation", action:"ajaxGetCategorie", params:"'base.id=' + zoptBase.value + '&aut.id=' + zoptAut.value", onComplete:"updateTypologie(e, 'cat.id', zoptCat.value)")};
				
				${remoteFunction(controller:"groupePrestation", action:"ajaxGetPrestation", params:"'grp.id=' + zoptGrp.value", onComplete:"updatePrestation(e)")};
			}
			
			function updateTypologie(e, field, sel) { // The response comes back as a bunch-o-JSON 
				
				var typos = eval("(" + e.responseText + ")") // evaluate JSON

				if (typos) { 
					var rselect = document.getElementById(field)
					
					// Clear all previous options 
					var l = rselect.length
					while (l > 0) { 
						rselect.remove(l) 
						l-- //PLacé après pour conserver la première valeur null
					}
				
					// Rebuild the select
					for (var i=0; i < typos.length; i++) {
						var typo = typos[i] 
						var opt = document.createElement('option'); 
						opt.text = typo.name 
						opt.value = typo.id 
						try { 
							rselect.add(opt, null) // standards compliant; doesn't work in IE 
						} catch(ex) { 
							rselect.add(opt) // IE only 
						} 
						if (typo.id==sel) {
							rselect.selectedIndex=i+1; // +1 si éléments null en première position sinon 0
						}
					}
				} 
			}
			
			function updatePrestation(e){
				var pres = eval("(" + e.responseText + ")") // evaluate JSON
				var inner=""
				if (pres) {

					for (var i=0; i < pres.length; i++) {
						var pre = pres[i]
						inner = inner + pre.groupe
					}
					var rshow = document.getElementById('pre.list')
					rshow.innerHTML=inner
				}
			}
		</g:javascript>
		
		
    </head>
    <body>
        <div class="nav">
            <span class="menuButton"><a class="home" href="${createLinkTo(dir: '')}"><g:message code="home" default="Home" /></a></span>
            <span class="menuButton"><g:link class="list" action="list"><g:message code="typologiePrestation.list" default="TypologiePrestation List" /></g:link></span>
            <span class="menuButton"><g:link class="create" action="create"><g:message code="typologiePrestation.new" default="New TypologiePrestation" /></g:link></span>
        </div>
        <div class="body">
            <h1><g:message code="typologiePrestation.edit" default="Edit TypologiePrestation" /></h1>
            <g:if test="${flash.message}">
            <div class="message"><g:message code="${flash.message}" args="${flash.args}" default="${flash.defaultMessage}" /></div>
            </g:if>
            <g:hasErrors bean="${typologiePrestationInstance}">
            <div class="errors">
                <g:renderErrors bean="${typologiePrestationInstance}" as="list" />
            </div>
            </g:hasErrors>
            <g:form method="post" >
                <g:hiddenField name="id" value="${typologiePrestationInstance?.id}" />
                <g:hiddenField name="version" value="${typologiePrestationInstance?.version}" />
				<g:hiddenField name="uniqueKey" value="${typologiePrestationInstance?.uniqueKey}" />
                <div class="dialog">
                    <table>
                        <tbody>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="base"><g:message code="typologiePrestation.base" default="Base" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologiePrestationInstance, field: 'base', 'errors')}${hasErrors(bean: typologiePrestationInstance, field: 'uniqueKey', 'errors')}">
                                    <g:select 
										name="base.id" 
										from="${ch.vd.dec.selt.petale.BaseLegale.list()}" 
										optionKey="id" 
										value="${typologiePrestationInstance?.base?.id}" 
										noSelection="['null': '']" 
										onchange="updateSelect()"
									/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="aut"><g:message code="typologiePrestation.aut" default="Aut" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologiePrestationInstance, field: 'aut', 'errors')}${hasErrors(bean: typologiePrestationInstance, field: 'uniqueKey', 'errors')}">
                                    <g:select 
										name="aut.id" 
										from="${ch.vd.dec.selt.petale.TypeAutorisation.list()}" 
										optionKey="id" 
										value="${typologiePrestationInstance?.aut?.id}" 
										noSelection="['null': '']" 
										onchange="updateSelect()"
									/>
                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="cat"><g:message code="typologiePrestation.cat" default="Cat" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologiePrestationInstance, field: 'cat', 'errors')}${hasErrors(bean: typologiePrestationInstance, field: 'uniqueKey', 'errors')}">
                                    <g:select 
										name="cat.id" 
										from="${ch.vd.dec.selt.petale.Categorie.list()}" 
										optionKey="id" 
										value="${typologiePrestationInstance?.cat?.id}" 
										noSelection="['null': '']" 
										onchange="updateSelect()"
									/>

                                </td>
                            </tr>
                        
                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="grp"><g:message code="typologiePrestation.grp" default="Grp" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologiePrestationInstance, field: 'grp', 'errors')}">
                                    <g:select 
										name="grp.id" 
										from="${ch.vd.dec.selt.petale.Groupe.list()}" 
										optionKey="id" 
										value="${typologiePrestationInstance?.grp?.id}"  
										onchange="updateSelect()"
									/>

                                </td>
                            </tr>
							
							<tr class="prop">
								<td valign="top" class="name"><g:message code="groupe.prestations.label" default="Prestations" /></td>
								
								<td valign="top" style="text-align: left;" class="value">
									<ul id="pre.list">
									<g:each in="${typologiePrestationInstance.grp.prestations}" var="p">
										<li>${p?.prestation?.encodeAsHTML()}</li>
									</g:each>
									</ul>
								</td>
								
							</tr>
							
							<tr class="prop">
                                <td valign="top" class="name"><g:message code="typologiePrestation.priorite" default="Priorite" />:</td>
                                
                                <td valign="top" class="value">${fieldValue(bean: typologiePrestationInstance, field: "priorite")}</td>
                                
                            </tr>
                        
 <!--                            <tr class="prop">
                                <td valign="top" class="name">
                                    <label for="priorite"><g:message code="typologiePrestation.priorite" default="Priorite" />:</label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologiePrestationInstance, field: 'priorite', 'errors')}">
                                    <g:select name="priorite" from="${8..1}" value="${typologiePrestationInstance?.priorite}"  />

                                </td>
                            </tr>
		 -->					
<!-- 							<tr class="prop">
                                <td valign="top" class="name">
                                  <label for="uniqueKey"><g:message code="typologiePrestation.uniqueKey.label" default="UniqueKey" /></label>
                                </td>
                                <td valign="top" class="value ${hasErrors(bean: typologiePrestationInstance, field: 'uniqueKey', 'errors')}">
                                    <g:textField name="uniqueKey" maxlength="30" value="${typologiePrestationInstance?.uniqueKey}" />
                                </td>
                            </tr>
  -->
                        </tbody>
                    </table>
                </div>
                <div class="buttons">
                    <span class="button"><g:actionSubmit class="save" action="update" value="${message(code: 'update', 'default': 'Update')}" /></span>
                    <span class="button"><g:actionSubmit class="delete" action="delete" value="${message(code: 'delete', 'default': 'Delete')}" onclick="return confirm('${message(code: 'delete.confirm', 'default': 'Are you sure?')}');" /></span>
                </div>
            </g:form>
        </div>
		<g:javascript>
			// This is called when the page loads to initialize
			updateSelect();
		</g:javascript>
    </body>
</html>
